package com.ry.est.util;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ry
 * @since 2021-11-09 08:56
 **/
public class BeanUtils {
    public static <S,T> T convertToDTO(S source,Class<T> clazz){
        try {
            T target = clazz.newInstance();
            org.springframework.beans.BeanUtils.copyProperties(source,target);

            return target;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static <S,T> List<T> convertToDTOList(List<S> sources,Class<T> clazz){
        List<T> dtos = new ArrayList<>();
        for (S source : sources) {
            T target = convertToDTO(source, clazz);
            dtos.add(target);
        }

        return dtos;
    }
}
